// pages/bookpeople/bookpeople.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import RegExp from "../../utils/regular"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: "",//名字
        phone: "",//手机号码
        certificates: "",//证件号码
        userInfo: [],
        buttonText:"保存"
    },
    onLoad(option) {
        // 获取confirm传过来的值
        let index = option.index
        let tag = option.number
        this.setData({
            tag
        })
        if(tag!= 0){
            console.log(1);
        this.data.username =wx.getStorageSync('userInfo')[index].username
        this.data.phone =wx.getStorageSync('userInfo')[index].phone
        this.data.certificates =wx.getStorageSync('userInfo')[index].certificates
        this.data.buttonText = "修改"
        this.setData({
            username:this.data.username,
            phone:this.data.phone ,
            certificates:this.data.certificates,
            buttonText:  this.data.buttonText 
        })
        }else{
            console.log(1);
        }
    },
    // 验证手机号
    phoneBlur() {
        if (RegExp.regular(this.data.phone)) {
        } else {
            Toast('请输入正确的手机号');
            return
        }
    },
    // 验证证件号码
    certificatesBlur() {
        if (RegExp.regCertificates(this.data.certificates)) {
        } else {
            Toast('请输入正确的证件号');
            return
        }
    },
    // 保存信息
    preservation() {
        // let userInfo = []
        let { username, phone, certificates } = this.data
        if (RegExp.regular(phone) && phone != "" && RegExp.regCertificates(certificates) && certificates != "") {
            let userInfo = wx.getStorageSync('userInfo') || []
            let ind = userInfo.findIndex((item) => {
                return item.username == username
            })
            if (ind == -1) {
                // 说明不存在
                userInfo.push({ username, phone, certificates })
                wx.setStorageSync('userInfo', userInfo)
                wx.showToast({
                    title: '保存成功',
                })
                //    15263021508  18369266113
                // 370404199709106219   370403199810311234
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1,
                    })
                }, 1500);
            } else {
                // 说明存在
                wx.showToast({
                    title: '此用户已经存在',
                    icon: "none"
                })
                return
            }
        } else {
            Toast('请输入完整且正确的信息');
        }
    },
    //返回上一个界面
    reture() {
        wx.navigateBack({
            delta: 1,
        })
    },
    // 点击右边了
    onClickRight() {
        wx.showToast({
            title: '点击右边了',
        })
        this.onClickIcon()
    }
})